package top.wu2020.lazy.ws.chat.infrastructure.persistence;

import top.wu2020.lazy.ws.chat.infrastructure.entity.HistorySessionDO;
import top.wu2020.lazy.ws.chat.infrastructure.converter.HistorySessionConverter;
import top.wu2020.lazy.ws.chat.infrastructure.mapper.HistorySessionMapper;
import top.wu2020.lazy.ws.chat.domain.model.history.session.HistorySessionRepository;
import org.springframework.stereotype.Repository;
import java.util.stream.Collectors;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import top.wu2020.lazy.ws.chat.domain.model.history.session.HistorySession;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import java.util.List;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
/**
 * describe history_session 
 *
 * @author Jia wei Wu
 * @date 2024/05/24 08:52 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence 
 **/
@Repository
public class HistorySessionRepositoryImpl  implements  HistorySessionRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增
     *
     * @param historySession 新增     
     * @return {@link Result<HistorySession>} 新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<HistorySession> story(HistorySession historySession) {
        HistorySessionDO historySessionDO = HistorySessionConverter.INSTANCE.fromHistorySession(historySession);
        lazyLambdaStream.upsert(historySessionDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增
     *
     * @param historySessionList 批量新增     
     * @return {@link Result<List<HistorySession>>} 新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<HistorySession>> batchStory(List<HistorySession> historySessionList) {
        List<HistorySessionDO> historySessionDOList = historySessionList.stream().map(HistorySessionConverter.INSTANCE::fromHistorySession).collect(Collectors.toList());
        lazyLambdaStream.upsert(historySessionDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个
     *
     * @param historySession 查询单个     
     * @return {@link Result<HistorySession>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<HistorySession> findOne(HistorySession historySession) {
        HistorySessionDO historySessionDO = HistorySessionConverter.INSTANCE.fromHistorySession(historySession);
        HistorySession historySessionOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(historySessionDO), HistorySession.class);
        return ResultFactory.successOf(historySessionOne);
    }

    /**
     * describe 查询多个
     *
     * @param historySession 查询多个     
     * @return {@link Result<List<HistorySession>>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<HistorySession>> findList(HistorySession historySession) {
        HistorySessionDO historySessionDO = HistorySessionConverter.INSTANCE.fromHistorySession(historySession);
        List<HistorySession> historySessionList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(historySessionDO), HistorySession.class);
        return ResultFactory.successOf(historySessionList);
    }

    /**
     * describe 分页查询多个
     *
     * @param size 当前页数
     * @param current 当前页
     * @param historySession 分页查询多个     
     * @return {@link Result<LazyPage<HistorySession>>} 分页领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<LazyPage<HistorySession>> findPage(int size,int current,HistorySession historySession) {
        HistorySessionDO historySessionDO = HistorySessionConverter.INSTANCE.fromHistorySession(historySession);
        LazyPage<HistorySession> lazyPage = new LazyPage<>(current,size);
        LazyPage<HistorySession> historySessionLazyPage = lazyLambdaStream.selectPage(LazyWrappers.lambdaWrapperBean(historySessionDO),lazyPage, HistorySession.class);
        return ResultFactory.successOf(historySessionLazyPage);
    }

    /**
     * describe 删除
     *
     * @param historySession 删除     
     * @return {@link Result<HistorySession>}      
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<HistorySession> remove(HistorySession historySession) {
        HistorySessionDO historySessionDO = HistorySessionConverter.INSTANCE.fromHistorySession(historySession);
        //  lazyLambdaStream.delete(LazyWrappers.lambdaWrapperBean(historySessionDO));
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在
     *
     * @param historySession 领域对象     
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Boolean> exists(HistorySession historySession) {
        HistorySessionDO historySessionDO = HistorySessionConverter.INSTANCE.fromHistorySession(historySession);
        Boolean exists=lazyLambdaStream.exists(LazyWrappers.lambdaWrapperBean(historySessionDO));
        return ResultFactory.successOf(exists);
    }

}